﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Modules;
    using System.Linq;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmDmBacSy : DevExpress.XtraEditors.XtraForm
    {
        GridCheckMarksSelection gridCheckMarksSelection = null;
        IList<DmBacSyEntity> lstDmBacSyEntity = null;

        DmBacSyBusiness dmBacSyBusiness = new DmBacSyBusiness();
        DmKhoaBusiness dmKhoaBusiness = new DmKhoaBusiness();

        public frmDmBacSy()
        {
            InitializeComponent();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDmBacSy);
            LoadDmKhoa();

        }

        void LoadDmKhoa()
        {
            var lstDmKhoaEntity = dmKhoaBusiness.GetDmKhoaList();
            lstDmKhoaEntity.Insert(0, new DmKhoaEntity() { MaKhoa = ConstantManagerClinic.sValueMember, TenKhoa = ConstantManagerClinic.sDisplayMember });
            lkDmKhoa.Properties.DataSource = lstDmKhoaEntity;
            lkDmKhoa.Properties.DisplayMember = "TenKhoa";
            lkDmKhoa.Properties.ValueMember = "MaKhoa";
            lkDmKhoa.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadData(object sender, EventArgs e)
        {
            lstDmBacSyEntity = dmBacSyBusiness.GetDmBacSyList();
            if (lkDmKhoa.EditValue.ToString() != ConstantManagerClinic.sValueMember)
                lstDmBacSyEntity = lstDmBacSyEntity.Where(n => n.MaKhoa == lkDmKhoa.EditValue.ToString()).ToList();

            grdDmBacSy.DataSource = lstDmBacSyEntity;
        }

        private void frmDmBacSy_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadData(sender, e);
        }

        private void lkDMKhoa_EditValueChanged(object sender, EventArgs e)
        {
            LoadData(sender, e);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }
            foreach (DmBacSyEntity item in deleteObjects)
            {
                dmBacSyBusiness.DeleteDmBacSy(item.MaBacSy);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadData(sender, e);
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmBacSy((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.dmBacSyEvent += new EventHandler(LoadData);
            frm.ShowDialog();
        }

        DmBacSyEntity GetRow()
        {
            if (gvDmBacSy.RowCount > 0)
            {
                return (DmBacSyEntity)gvDmBacSy.GetFocusedRow();
            }

            return null;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var dmBacSyEntity = GetRow();
            if (dmBacSyEntity == null) return;
            var frm = new frmCapNhatDmBacSy((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmBacSyEntity);
            frm.dmBacSyEvent += new EventHandler(LoadData);
            frm.ShowDialog();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gvDMBacSy_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void grdDMbacsy_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;
            btnSua_Click(sender, e);
        }


    }
}